package com.inter.insideinterservice.newservice;

import com.inter.entity.newpojo.Constant;
import com.inter.entity.newpojo.TurntoInterfaceUtils;
import com.inter.entity.newpojo.request.RequestBody.QueryOrderRequestBody;
import com.inter.entity.newpojo.request.subRequest.QueryOrderRequest;
import com.inter.entity.newpojo.response.responseBody.QueryOrderResponseBody;
import com.inter.entity.newpojo.request.RequestHeader;
import com.inter.entity.newpojo.response.subResponse.QueryOrderResponse;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Demo 去北京查询当前设备的订单
 *
 * @author lm
 * @date 2018/08/31
 */
public class TrunToQueryorderService {

    private static Logger logger = Logger.getLogger(TrunToQueryorderService.class);

    public QueryOrderResponseBody queryOrder(String partnerCode, String deviceId, String orderType, String orderId, String orderStatus, String secret, Integer pageNo, Integer pageSize) {

        try {

            QueryOrderRequestBody body = new QueryOrderRequestBody();

            if (StringUtils.isNotBlank(deviceId)) {
                body.setDeviceId(deviceId);
            }
            if (StringUtils.isNotBlank(orderType)) {
                body.setOrderId(orderType);
            }
            if (StringUtils.isNotBlank(orderId)) {
                body.setOrderType(orderId);
            }
            if (StringUtils.isNotBlank(orderStatus)) {
                body.setOrderStatus(orderStatus);
            }
            if (pageNo != null) {
                body.setPageNo(pageNo.toString());
            } else {
                body.setPageNo("1");
            }
            if (pageNo != null) {
                body.setPageSize(pageSize.toString());
            } else {
                body.setPageSize("20");
            }
            // 请求参数拼接
            QueryOrderRequest request = new QueryOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_queryOrder, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QueryOrderRequest.class).toXml(request, "utf-8");
            //logger.info("请求xml报文：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //logger.info("响应报文：" + result);
            QueryOrderResponse responseObj = new JaxbUtil(QueryOrderResponse.class).fromXml(result, "utf-8");


            if (responseObj.getHeader().getResultCode().equalsIgnoreCase(Constant.RET_CODE_0000) == true) {
                QueryOrderResponseBody re = responseObj.getBody();
                return re;

            } else {

                QueryOrderResponseBody re = responseObj.getBody();
                return re;

            }


        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }


    public QueryOrderResponseBody queryOrderOld(String partnerCode, String deviceId, String orderType, String orderId, String orderStatus, String secret) {
        try {

            QueryOrderRequestBody body = new QueryOrderRequestBody();

            if (StringUtils.isNotBlank(deviceId)) {
                body.setDeviceId(deviceId);
            }
            if (StringUtils.isNotBlank(orderType)) {
                body.setOrderId(orderType);
            }
            if (StringUtils.isNotBlank(orderId)) {
                body.setOrderType(orderId);
            }
            if (StringUtils.isNotBlank(orderStatus)) {
                body.setOrderStatus(orderStatus);
            }
            // 请求参数拼接
            QueryOrderRequest request = new QueryOrderRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_queryOrder, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(QueryOrderRequest.class).toXml(request, "utf-8");
            //logger.info("请求xml报文：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //logger.info("响应报文：" + result);
            QueryOrderResponse responseObj = new JaxbUtil(QueryOrderResponse.class).fromXml(result, "utf-8");

            if (true == Constant.RET_CODE_0000.equalsIgnoreCase(responseObj.getHeader().getResultCode())) {
                QueryOrderResponseBody res = responseObj.getBody();
                return res;
            } else {

                QueryOrderResponseBody res = responseObj.getBody();
                return res;

            }


        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;

    }
}
